﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * 
 * */

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using QWModel;
using QWTouchPanel.Model;
using QWTouchPanel.Properties;

namespace QWTouchPanel
{
    /// <summary>
    /// TouchUI : Form
    /// </summary>
    public sealed partial class TouchUI : Form
    {
        /// <summary>
        /// QWEntryPrint
        /// </summary>
        private QWEntryPrint _qwBerkas;

        /// <summary>
        /// QWClock
        /// </summary>
        private QWClock _qwClock;

        /// <summary>
        /// QWKasirPrint
        /// </summary>
        private QWKasirPrint _qwKasir;

        /// <summary>
        /// QWSearch
        /// </summary>
        private QWSearch _qwSearch;

        /// <summary>
        /// Initializes a new instance of the <see cref="TouchUI"/> class.
        /// </summary>
        public TouchUI()
        {
            InitializeComponent();

            // set BACKGROUND
            var tb = new Thread(SetBackgroundImage);
            tb.Start();

            // if using CR Layout for printing
            if (Settings.Default.PrintingLayout == 1)
            {
                var tr = new Thread(LoadCRLayout);
                tr.Start();
            }
        }

        /// <summary>
        /// Sets the background image.
        /// </summary>
        private void SetBackgroundImage()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(SetBackgroundImage);
                Invoke(o);
            }
            else
            {
                BackgroundImage = Resources.kde;
            }
        }

        /// <summary>
        /// Loads the CR layout.
        /// </summary>
        private void LoadCRLayout()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(LoadCRLayout);
                Invoke(o);
            }
            else
            {
                var crDocument = new ReportDocument();
                crDocument.Load(Application.StartupPath + "/Reports/PrintNomor.Rpt");
                crDocument.Close();
            }
        }

        /// <summary>
        /// Shows the time widget.
        /// </summary>
        private void TClock()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TClock);
                Invoke(o);
            }
            else
            {
                _qwClock = new QWClock();
                _qwClock.Location = new Point(Width - _qwClock.Width, 10);
                Controls.Add(_qwClock);
            }
        }

        /// <summary>
        /// Initializes the search widget.
        /// </summary>
        private void InitializeSearchWidget()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(InitializeSearchWidget);
                Invoke(o);
            }
            else
            {
                _qwSearch = new QWSearch {Location = new Point(10, 10)};
                Controls.Add(_qwSearch);
            }
        }

        /// <summary>
        /// Initalizes the penyerahan berkas widget.
        /// </summary>
        private void InitalizePenyerahanBerkasWidget()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(InitalizePenyerahanBerkasWidget);
                Invoke(o);
            }
            else
            {
                _qwBerkas = new QWEntryPrint();
                _qwBerkas.Location = new Point(10, Height - _qwBerkas.Height + 15);
                Controls.Add(_qwBerkas);
            }
        }

        /// <summary>
        /// Initializes the kasir widget.
        /// </summary>
        private void InitializeKasirWidget()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(InitializeKasirWidget);
                Invoke(o);
            }
            else
            {
                _qwKasir = new QWKasirPrint();
                _qwKasir.Location = new Point(Width - _qwKasir.Width, Height - _qwKasir.Height + 15);
                Controls.Add(_qwKasir);
            }
        }

        /// <summary>
        /// Handles the Shown event of the TouchUI control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TouchUI_Shown(object sender, EventArgs e)
        {

            var t1 = new Thread(TClock);
            t1.Start();

            var t2 = new Thread(InitializeSearchWidget);
            t2.Start();

            var t3 = new Thread(InitalizePenyerahanBerkasWidget);
            t3.Start();

            var t4 = new Thread(InitializeKasirWidget);
            t4.Start();

            if (!AuthenticateUser(this))
            {
                Application.Exit();
            }
        }

        /// <summary>
        /// Authenticates the user.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        public bool AuthenticateUser(IWin32Window parent)
        {
            DialogResult dlg;
            using (var f = new AccessCodeUI())
            {
                dlg = f.ShowDialog(parent);
            }

            return (dlg.Equals(DialogResult.OK));
        }

        /// <summary>
        /// Handles the KeyDown event of the TouchUI control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
        private void TouchUI_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    Application.Exit();
                    break;
                case Keys.F12:
                    Application.Restart();
                    break;
            }
        }

        #region Nested type: DelegateThis

        /// <summary>
        /// DelegateThis
        /// </summary>
        private delegate void DelegateThis();

        #endregion
    }
}